#!/usr/bin/env bash
set -euo pipefail

# Check for root
if [[ $EUID -ne 0 ]]; then
    echo "Please run this script as root"
    exit 1
fi

# Bypass local DNS
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

# Prepare the system
ufw disable
ip6tables --list >/dev/null
iptables -F
sysctl -w net.ipv4.conf.all.route_localnet=1
sysctl -w net.ipv4.ip_forward=1
sysctl -w net.bridge.bridge-nf-call-iptables=1
sysctl -w fs.inotify.max_user_watches=1048576
iptables -t nat -I POSTROUTING -s 127.0.0.0/8 ! -d 127.0.0.0/8 -j MASQUERADE

# Assume we're running on this arch
ARCH=amd64

# Global vars to be used
# shellcheck source=vars
source "$(dirname "${BASH_SOURCE[0]}")"/vars

pushd "$ARCHIVE_PATH"

# Untar the bundle
tar xfvz "$ARCHIVE"
pushd cri-o

# Install and prepare config
./install

# Use runc as default runtime
rm /etc/crio/crio.conf.d/10-crun.conf
cat <<EOT >>/etc/crio/crio.conf.d/10-config.conf
[crio.runtime]
log_level = "debug"

[crio.runtime.runtimes.runc]
runtime_path = "/usr/local/bin/runc"
EOT

runc --version
conmon --version

crio config
crio config --default

# Start CRI-O
systemctl daemon-reload
systemctl start crio
systemctl is-active --quiet crio && echo CRI-O is running
journalctl --no-pager -u crio

# Start Kubernetes
GOROOT="$GOROOT_1_16_X64"
GOPATH="$HOME/go"
PATH="$GOROOT/bin:$PATH"
K8SPATH="$GOPATH/src/k8s.io"
mkdir -p "$K8SPATH"
pushd "$K8SPATH"
git clone --depth=1 https://github.com/kubernetes/kubernetes
pushd kubernetes

LATEST=$(curl -sSfL https://dl.k8s.io/ci/latest.txt)
echo "Using Kubernetes build $LATEST"

URL="https://dl.k8s.io/ci/$LATEST"
mkdir -p _output/bin

echo Downloading server binaries
curl -sSfL "$URL/kubernetes-server-linux-amd64.tar.gz" -o- | tar xfz -
cp kubernetes/server/bin/{kubectl,kube-apiserver,kube-controller-manager,kube-proxy,kube-scheduler,kubelet} \
    _output/bin

echo Downloading test binaries
curl -sSfL "$URL/kubernetes-test-linux-amd64.tar.gz" -o- | tar xfz -
cp kubernetes/test/bin/{ginkgo,e2e.test} _output/bin

export CONTAINER_RUNTIME=remote
export CGROUP_DRIVER=systemd
export CGROUP_ROOT=/
export KUBELET_FLAGS='--runtime-cgroups=/system.slice/crio.service --non-masquerade-cidr=0.0.0.0/0'
export CONTAINER_RUNTIME_ENDPOINT=/var/run/crio/crio.sock
export ALLOW_PRIVILEGED=1

IP=$(ip route get 1.2.3.4 | cut -d ' ' -f7 | tr -d '[:space:]')
echo "Using IP: $IP"
export DNS_SERVER_IP=$IP
export API_HOST_IP=$IP

hack/install-etcd.sh
export PATH="$GOPATH/src/k8s.io/kubernetes/third_party/etcd:$PATH"

OUTPUT=$(mktemp)
hack/local-up-cluster.sh -O 2>&1 | tee "$OUTPUT" &
PID=$!

until grep -q "Local Kubernetes cluster is running" "$OUTPUT"; do
    echo Waiting for hack/local-up-cluster.sh
    if ! ps $PID >/dev/null; then
        exit 1
    fi
    sleep 5
done

echo Cluster is up and running

# Run the tests
export KUBERUN=/var/run/kubernetes
export KUBECONFIG=$KUBERUN/admin.kubeconfig
export PATH="$GOPATH/src/k8s.io/kubernetes/_output/local/bin/linux/amd64:$PATH"
export KUBE_MASTER_URL=$IP
export KUBE_MASTER_IP=$IP
export KUBE_MASTER=$IP

export GINKGO_PARALLEL_NODES=4
export GINKGO_PARALLEL=y

_output/bin/e2e.test --provider=local --host="https://$IP:6443" \
    --ginkgo.focus='\[NodeConformance|NodeFeature:.*\]' \
    --ginkgo.skip='\[Flaky|Slow|Serial|sig-network|NodeFeature:RuntimeHandler\]|exec hook properly'
